﻿using System;
using WebSocketSharp;
using WebSocketSharp.Server;
using System.Threading;

namespace OperateCamera
{
    class websocket
    {

        public static void Run()
        {

            string Addr = string.Format("ws://{0}:8080", common.GetLocalIP());

           

            var wssv = new WebSocketServer(Addr);

            wssv.AddWebSocketService<GetImage>("/GetImage");

            wssv.Start();
            Console.ReadKey(true);

            wssv.Stop();

        }

    }

    //响应信息 
    public class GetImage : WebSocketBehavior
    {




        protected override void OnMessage(MessageEventArgs e)
        {
            Console.WriteLine(e.Data);


            if (e.Data == "start")
            {

                common.webCamStatusOpen = true;

            }
            else
            {

                common.webCamStatusOpen = false;
            }


            try
            {
                new Thread(new ThreadStart(delegate {


                    byte[] img2 = new byte[1];
                    while (true)
                    {

                        if (common.webCamStatusOpen == false)
                        {

                            break;
                        }

                        img2 = common.app.Tinvoke();

                        Send(img2);

                        Thread.Sleep(500);

                    }

                })).Start();


            }
            catch (Exception FileIOException)
            {



            }


        }




    }








}
